package com.gys;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.gys.common.util.SpringUtil;
import com.gys.view.LoginView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import com.gys.common.exception.BusinessException;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableCaching
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, PageHelperAutoConfiguration.class})
@EnableAsync
@EnableScheduling
public class GysApplication extends AbstractJavaFxApplicationSupport {
    private static final Logger log = LoggerFactory.getLogger(GysApplication.class);

//    @Override
//    public void start(Stage primaryStage) throws Exception{
//        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
//        primaryStage.setTitle("Hello World");
//        primaryStage.setScene(new Scene(root, 300, 275));
//        primaryStage.show();
//        LoginView view = SpringUtil.getBean(LoginView.class);
//        Stage stage = (Stage)view.getView().getScene().getWindow();
//        primaryStage=stage;
//        primaryStage.show();
//    }


    public static void main(String[] args) {
        Thread.setDefaultUncaughtExceptionHandler(GysApplication::saveExceptionLog);
        GysApplication.launch(GysApplication.class, LoginView.class, (String[])args);
        //launch(args);
    }

    private static void saveExceptionLog(Thread t, Throwable e) {
        log.error((Platform.isFxApplicationThread() ? "javafx应用线程异常：" : "其他应用线程异常：") + t);
        log.error("异常信息", e);
        if (e.getClass() == BusinessException.class) {
            if (ObjectUtil.isNotEmpty(e.getMessage())) {
                GysApplication.showErrorAlert(e.getMessage());
            }
            return;
        }
        if (ObjectUtil.isNotEmpty(e.getCause())) {
            try {
                Throwable throwable = ((InvocationTargetException)e.getCause()).getTargetException();
                if (throwable.getClass() == BusinessException.class) {
                    GysApplication.showErrorAlert(throwable.getMessage());
                    return;
                }
            }
            catch (Exception exception) {
                // empty catch block
            }
        }
        GysApplication.showErrorAlert("系统异常，请联系系统管理原！");
    }

    public static void showErrorAlert(String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("异常信息");
        alert.setContentText(message);
        alert.showAndWait();
    }
}
